package com.dmz.workflow.quartz;

import com.dmz.workflow.constant.MsgContentEnum;
import com.dmz.workflow.dto.OneHourAlert;
import com.dmz.workflow.service.WorkflowNodeExtService;
import com.dmz.workflow.util.ImcComponent;
import com.dmz.workflow.util.MessageContentWrapper;
import com.dmz.workflow.util.ObjectUtils;
import com.dmz.workflow.entity.StaffInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class MessageNotice {

    @Autowired
    private WorkflowNodeExtService workflowNodeExtService;

    @Autowired
    private ImcComponent imcComponent;

    @Autowired
    private MessageContentWrapper messageContentWrapper;

    @Scheduled(cron = "0/59 * * * * ?")
    public void execute() {
        List<OneHourAlert> oneHourAlerts = workflowNodeExtService.selectStaffListToInformToApprove();
        if (!ObjectUtils.validateCollectionIsEmpty(oneHourAlerts)) {
            List<StaffInfo> staffInfos = new ArrayList<>();
            for (OneHourAlert oneHourAlert : oneHourAlerts) {
                StaffInfo staffInfo = new StaffInfo();
                staffInfo.setId(oneHourAlert.getId());
                staffInfo.setMessageCount(1);
                staffInfo.setCreateTime(oneHourAlert.getCreateTime());
                staffInfos.add(staffInfo);
                if (ObjectUtils.isNotEmpty(oneHourAlert.getUserId())) {
                    imcComponent.imcAssistantSender(
                            messageContentWrapper.generateNotCommentMessageBody(MsgContentEnum.MESSAGE_ONE_HOUR_TIMER_CONTENT, oneHourAlert.getProcessInstanceId()),
                            null,
                            Collections.singletonList(oneHourAlert.getUserId()),
                            null
                    );
                }
            }
            workflowNodeExtService.updateBatchById(staffInfos);
        }
    }
}
